package com.cuit.eduservice.controller;


import com.cuit.eduservice.client.VodClient;
import com.cuit.eduservice.entity.EduChapter;
import com.cuit.eduservice.entity.EduVideo;
import com.cuit.eduservice.service.EduVideoService;
import com.cuit.exception.MyException;
import com.cuit.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author horizon
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    EduVideoService videoService;

    //注入vodClient
    @Autowired
    VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    //根据id查询
    @GetMapping("/getVideoInfo/{id}")
    public R getChapterInfo(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        return R.ok().data("video",video);
    }
    //修改小结
    @PostMapping("/updateVideo")
    public R updateChapter(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }
    //删除小节,删除阿里云中视频
    @DeleteMapping("deleteVideo/{id}")
    public R deleteVideo(@PathVariable("id") String id){
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId=eduVideo.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            R result = vodClient.deleteByAlyId(videoSourceId);
            if(result.getCode()==20001){
                throw new MyException(20001,"删除视频失败，熔断器");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }

}

